package com.hardy.demo.proxy;

import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

/**
 * Author: Hardy
 * Date:   2021/3/11
 * Description:
 **/
public class Main {

    public static void main(String[] args) {
        //jdkProxy();

        cglibProxy();
    }

    private static void jdkProxy() {
        JdkProxyUserOption proxy = new JdkProxyUserOption(new UserOption());
        Option opt = (Option) Proxy.newProxyInstance(Option.class.getClassLoader(), UserOption.class.getInterfaces(), proxy);

        opt.setName("aaa");
        System.out.println(opt.getName());
    }

    private static void cglibProxy() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(UserOption.class);
        enhancer.setCallback(new CglibProxyUserOption());

        Option opt = (Option) enhancer.create();

        opt.setName("aaa");
        System.out.println(opt.getName());
    }
}
